﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

                    xmlns:selector="clr-namespace:MiniUML.Plugins.UmlClassDiagram.TemplateSelector"
                    
                    xmlns:connect="clr-namespace:MiniUML.Plugins.UmlClassDiagram.Controls.View.Connect"
                    xmlns:viewconv="clr-namespace:MiniUML.View.Converter;assembly=MiniUML.View"
                    xmlns:controls="clr-namespace:MiniUML.View.Controls;assembly=MiniUML.View"
                    >

  <Style TargetType="{x:Type connect:GenericUmlAssociation}"
         xmlns:views="clr-namespace:MiniUML.View.Views;assembly=MiniUML.View"
         >
    <Setter Property="Focusable" Value="True" />
    <Setter Property="SnapsToDevicePixels" Value="True"/>
    <Setter Property="views:CanvasView.CustomDrag" Value="True"/>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type connect:GenericUmlAssociation}"
                          x:Name="_this"
                          >
          <ControlTemplate.Resources>
            <selector:ArrowTemplateSelector x:Key="ArrowTemplateSelector"/>

            <viewconv:ShapeIdToControlConverter x:Key="ShapeIdToControlConverter"
                                                ReferenceControl="{Binding RelativeSource={RelativeSource AncestorType={x:Type connect:GenericUmlAssociation}}}" />
          </ControlTemplate.Resources>
          <Canvas>
            <Canvas.Resources>
              <!-- Set the style of the ThreePieceLine's Polyline.-->
              <Style TargetType="Polyline">
                <!-- Setter Property="Stroke" Value="{Binding ElementName='_this', Path='Stroke'}"/ -->
                <Setter Property="Stroke" Value="{Binding Path=Stroke, RelativeSource={RelativeSource AncestorType={x:Type connect:GenericUmlAssociation}}}"/>
              </Style>

              <!-- Set the style of arrow heads (whose style must be {DynamicResource StrokeStyle}). -->
              <Style x:Key="StrokeStyle" TargetType="Shape">
                <!-- Setter Property="Stroke" Value="{Binding ElementName='_this', Path='Stroke'}"/ -->
                <Setter Property="Stroke" Value="{Binding Path=Stroke, RelativeSource={RelativeSource AncestorType={x:Type connect:GenericUmlAssociation}}}"/>
              </Style>
            </Canvas.Resources>

            <connect:ThreePieceLine x:Name="tpl"
                                    FromX="{Binding Path='FirstNode.Left'}"
                                    FromY="{Binding Path='FirstNode.Top'}"
                                    FromOrientation="{Binding ElementName='FromAnchor', Path='SnapOrientation'}"
                                    ToX="{Binding Path='LastNode.Left'}"
                                    ToY="{Binding Path='LastNode.Top'}"
                                    ToOrientation="{Binding ElementName='ToAnchor', Path='SnapOrientation'}"
                                      
                                    Canvas.Top="0"
                                   />
            <!-- SnapTargetUpdate="snapTargetUpdate"
                                    
    -->

            <!-- Line Stroke="Red"
          StrokeThickness="2"
          X1="{Binding Path='FirstNode.Left'}" Y1="{Binding Path='FirstNode.Top'}"
          X2="{Binding Path='LastNode.Left'}"  Y2="{Binding Path='LastNode.Top'}">
          </Line -->

            <ContentControl ContentTemplateSelector="{StaticResource ArrowTemplateSelector}"
                            Canvas.Left="{Binding Path=FirstNode.Left}"
                            Canvas.Top="{Binding Path=FirstNode.Top}">
              <ContentControl.RenderTransform>
                <RotateTransform x:Name="roteFromAngleInDegrees" Angle="{Binding ElementName=tpl, Path=FromAngleInDegrees}"/>
              </ContentControl.RenderTransform>
                <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FromArrow"/>
            </ContentControl>

            <ContentControl ContentTemplateSelector="{StaticResource ArrowTemplateSelector}"
                            Canvas.Left="{Binding Path=LastNode.Left}"
                            Canvas.Top="{Binding Path=LastNode.Top}">
              <ContentControl.RenderTransform>
                <RotateTransform x:Name="ToAngleInDegrees" Angle="{Binding ElementName=tpl, Path=ToAngleInDegrees}"/>
              </ContentControl.RenderTransform>
              <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="ToArrow"/>
            </ContentControl>

            <controls:AnchorPoint x:Name="FromAnchor"
                                  Canvas.Left="{Binding Path=FirstNode.Left}"
                                  Canvas.Top="{Binding Path=FirstNode.Top}"
                                  Left="{Binding Path=FirstNode.Left}"
                                  Top="{Binding Path=FirstNode.Top}"
                                  SnapTarget="{Binding Path=From, Converter={StaticResource ShapeIdToControlConverter}}"/>

            <controls:AnchorPoint x:Name="ToAnchor"
                                  Canvas.Left="{Binding Path=LastNode.Left}"
                                  Canvas.Top="{Binding Path=LastNode.Top}"
                                  Left="{Binding Path=LastNode.Left}"
                                  Top="{Binding Path=LastNode.Top}"
                                  SnapTarget="{Binding Path=To, Converter={StaticResource ShapeIdToControlConverter}}"/>

          </Canvas>

        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>


</ResourceDictionary>